package org.dsc.amafinder;

import org.dsc.amafinder.dialog.CloseAppDialogFragment;
import org.dsc.amafinder.model.AmfQR;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class AMain extends Activity {
   private static final String TAG = "afr." + AMain.class.getSimpleName();

   private EditText descuentoToSearch;
   private EditText articuloToSearch;
   private Button btnSearch;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      Log.i(TAG, "onCreate::" + toString());
      setContentView(R.layout.asearch);

      btnSearch = (Button) findViewById(R.id.btnSearch);
      descuentoToSearch = (EditText) findViewById(R.id.descuentoToSearch);
      articuloToSearch = (EditText) findViewById(R.id.articuloToSearch);

      btnSearch.setOnClickListener(new OnClickListener() {
         @Override
         public void onClick(View v) {
            String url = AmfQR.formatUrl(descuentoToSearch.getText().toString(), articuloToSearch.getText().toString());
            lanzar(url);
         }
      });
   }
   
   protected void lanzar(String url) {
      startActivity(new Intent(this, AWebView.class));
   }
   
   protected void salir() {
      CloseAppDialogFragment closeDlg = CloseAppDialogFragment.newInstance(this);
      closeDlg.show(getFragmentManager(), "Alert");
      // CustomDialog customDialog = CustomDialog.newInstance(getParent());
      // customDialog.show();      
   }   

   @Override
   public boolean onKeyDown(int keyCode, KeyEvent event) {
      Log.i(TAG, "onKeyDown::" + toString());
      if (keyCode == KeyEvent.KEYCODE_BACK) {
         salir();
      }
      // para las demas cosas, se reenvia el evento al listener habitual
      return super.onKeyDown(keyCode, event);
   }

   /*
    * @ini-menu-action-bar
    */
   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu items for use in the action bar
      getMenuInflater().inflate(R.menu.main_activity_actions, menu);
//      menu.findItem(R.id.action_search).setIntent(new Intent(this, AMain.class));
      menu.findItem(R.id.action_options).setIntent(new Intent(this, AOptions.class));
      menu.findItem(R.id.action_info).setIntent(new Intent(this, AInfo.class));
      return super.onCreateOptionsMenu(menu);
   }

   @Override
   public boolean onOptionsItemSelected(MenuItem item) {
      switch (item.getItemId()) {
      case R.id.action_options:
      case R.id.action_info:
         startActivity(item.getIntent());
         break;
      default:
         return super.onOptionsItemSelected(item);
      }
      return true;
   }
   /*
    * @end-menu-action-bar
    */
}
